// pages/custom/makeself/makeself.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    conditions: [],
    names: '',
    sexval: '',
    sex:'',
    duties: '',
    hospital: '',
    phone:'',
    sexArr:[
      '女','男'
    ],
    pics:[],
    licenseurl:'',
    referee:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var temval = res.windowWidth - 60;
        temval = temval / 4;
        that.setData({
          picwh: temval
        });
      },
    });
    console.log(options);
    if (!options.scene){
      wx.showToast({
        title: '参数错误',
        icon: 'none'
      })
    }
    else{
      this.setData({
        referee: options.scene
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.shareToIndex();
  },
  uploadPic: function () {
    var that = this;
    var url = app.globalData.webUrl + '/index.php/api/Distribut/return_distribut_upload';
    var pics = that.data.pics;
    wx.uploadFile({
      url: url,
      filePath: pics[0],
      name: 'file',
      success: function (res) {
        res = JSON.parse(res.data);
        if (res.status == 'ok') {
          console.log(res.result);
          that.setData({
            licenseurl: res.result
          });
          that.SaveCustomList();
        }
      },
      fail: function (res) {
      }
    })
  },
  uploadImg: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var imgurls = res.tempFilePaths;
        var pics = that.data.pics;
        for (var i = 0; i < imgurls.length; i++) {
          if (pics.length >= 1) {
            break;
          }
          pics.push(imgurls[i]);
        }
        that.setData({
          pics: pics
        });
      },
    });
  },
  removePic: function (e) {
    var picindex = e.currentTarget.dataset.picindex;
    var pics = this.data.pics;
    pics.splice(picindex, 1);
    this.setData({
      pics: pics
    });
  },
  showPic: function (e) {
    var imgurl = e.currentTarget.dataset.curpic;
    wx.previewImage({
      urls: [imgurl],
      current: imgurl
    });
  },
  saveInfoAction: function () {
    if (this.checkCondition() == false){
      return;
    }

    app.ready(() => {
      wx.showLoading({
        title: '',
      });
      this.uploadPic();
    });
    app.error(err => {
      if (!app.globalData.userWxInfo) {
        wx.navigateTo({
          url: '/pages/auth/auth',
        });
      }
    });
  },
  notesInput: function (event) {
    var itype = event.currentTarget.dataset.itype;
    if (itype == '1') {
      this.data.names = event.detail.value;
    }
    else if (itype == '3') {
      this.data.duties = event.detail.value;
    }
    else if (itype == '4') {
      this.data.hospital = event.detail.value;
    }
    else if (itype == '5') {
      this.data.phone = event.detail.value;
    }
  },
  notesInputatt: function (event) {
    var itemindex = event.currentTarget.dataset.itemindex;
    var itype = event.currentTarget.dataset.itype;
    console.log('itemindex=' + itemindex);
    var values = event.detail.value;
    var conditions = this.data.conditions;

    if (itype == '5') {
      console.log('key=' + values);
      this.data.conditions[itemindex].attname = values;
    }
    else if (itype == '6') {
      console.log('va=' + values);
      this.data.conditions[itemindex].attvalue = values;
    }
  },
  SaveCustomList: function (offset) {
    var url = '/index.php/api/Distribut/examineFrom';
    var params = {
      names: this.data.names,
      sex: this.data.sex,
      phone: this.data.phone,
      hospital: this.data.hospital,
      duties: this.data.duties,
      ruid: this.data.referee,
      licenseurl: this.data.licenseurl
    }
    app.request(url, params).then(this.SaveCoutomCallBack.bind(this));
  },
  SaveCoutomCallBack: function (res) {
    // console.log(res);
    wx.hideLoading();
    if (res.status == 'ok') {
      wx.showToast({
        title: '提交成功',
        duration: 2000,
      });

      var delayid = setTimeout(function () {
        clearTimeout(delayid);
        app.toUserCenter();
      }, 2000);
    }
    else {
      wx.showToast({
        title: '提交失败：' + res.msg,
        icon: 'none',
        duration: 3000,
      });
    }
  },
  bindSexChange: function (e) {
    console.log(e.detail.value);
    this.setData({
      sex: e.detail.value,
      sexval: this.data.sexArr[e.detail.value]
    });
  },
  checkCondition:function(){
    if (!this.data.names) {
      wx.showToast({
        title: '姓名未填写',
        icon: 'none',
        duration: 3000,
      });
      return false;
    }
    if (!this.data.sex) {
      wx.showToast({
        title: '性别未选择',
        icon: 'none',
        duration: 3000,
      })
      return false;
    }
    if (!this.data.phone) {
      wx.showToast({
        title: '手机号码未填写',
        icon: 'none',
        duration: 3000,
      });
      return false;
    }
    else{
      if (!app.isPhoneAvailable(this.data.phone)){
        wx.showToast({
          title: '无效的手机号码',
          icon: 'none',
          duration: 3000,
        });
        return false;
      }
    }
    if (!this.data.duties) {
      wx.showToast({
        title: '职位未填写',
        icon: 'none',
        duration: 3000,
      });
      return false;
    }
    if (!this.data.hospital) {
      wx.showToast({
        title: '就职医院未填写',
        icon: 'none',
        duration: 3000,
      });
      return false;
    }
    if (this.data.pics.length <= 0) {
      wx.showToast({
        title: '医生执照未上传',
        icon: 'none',
        duration: 3000,
      });
      return false;
    }
    return true;
  }
})